package com.drinks.yiniang.adapter.listview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.drinks.yiniang.R;

/**
 * @author han
 * @date 2020/1/15
 */
public class PayPasswordKeyBoardAdapter extends BaseAdapter {

    private static final String NUMBERS = "123456789C0#";

    private Context mContext;
    private Drawable mDrawable;

    public PayPasswordKeyBoardAdapter(Context mContext) {
        this.mContext = mContext;

        mDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_pwd_delete);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;

        if (convertView == null) {
            mHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pay_password_keyboard, null);

            mHolder.fl_pay_pwd_keyboard = convertView.findViewById(R.id.fl_pay_pwd_keyboard);
            mHolder.tv_pay_pwd_keyboard = convertView.findViewById(R.id.tv_pay_pwd_keyboard);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        String curNumber = (String) getItem(position);

        if ("C".equals(curNumber)) {
            mHolder.tv_pay_pwd_keyboard.setText(curNumber);
            mHolder.fl_pay_pwd_keyboard.setBackgroundColor(Color.TRANSPARENT);

            setDrawable(mHolder.tv_pay_pwd_keyboard, false);
        } else if ("#".equals(curNumber)) {
            mHolder.tv_pay_pwd_keyboard.setText("");
            mHolder.fl_pay_pwd_keyboard.setBackgroundColor(Color.TRANSPARENT);

            setDrawable(mHolder.tv_pay_pwd_keyboard, true);
        } else {
            mHolder.tv_pay_pwd_keyboard.setText(curNumber);
            mHolder.fl_pay_pwd_keyboard.setBackgroundResource(R.drawable.selector_pay_pwd_keyboard);

            setDrawable(mHolder.tv_pay_pwd_keyboard, false);
        }

        return convertView;
    }

    private void setDrawable(TextView tv, boolean hasDraw) {
        tv.setCompoundDrawablesWithIntrinsicBounds(hasDraw ? mDrawable : null, null, null, null);
    }

    @Override
    public int getCount() {
        return NUMBERS.length();
    }

    @Override
    public Object getItem(int position) {
        return String.valueOf(NUMBERS.charAt(position));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder {
        FrameLayout fl_pay_pwd_keyboard;
        TextView tv_pay_pwd_keyboard;
    }
}
